package com.ukao.steward.ui.logistics.LeverFactory;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.ukao.steward.R;
import com.ukao.steward.adapter.LeveFactoryAdapter;
import com.ukao.steward.base.IAdapter;
import com.ukao.steward.base.IPage;
import com.ukao.steward.base.MvpFragment;
import com.ukao.steward.base.PageWrapper;
import com.ukao.steward.bean.EnterFactoryBean;
import com.ukao.steward.pesenter.logistics.LeverFactoryPesenter;
import com.ukao.steward.ui.logistics.EnterFactory.EnterFactoryTabFragment;
import com.ukao.steward.ui.valetRunners.takeLetter.TakeLetterTabFragment;
import com.ukao.steward.util.CheckUtils;
import com.ukao.steward.view.logistics.LeverFactoryView;
import com.ukao.steward.widget.MyLRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 离厂
 */
public class LeverFactoryFragment extends MvpFragment<LeverFactoryPesenter> implements  LeverFactoryView {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.lrecycler_view)
    MyLRecyclerView lrecyclerView;
    Unbinder unbinder;

    // TODO: Rename and change types of parameters
    private int enterType;
    private String mParam2;
    private List<EnterFactoryBean.ListBean> mData;
    private LeveFactoryAdapter mLeveFactoryAdapter;
    private PageWrapper mPageWrapper;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private View mView;
    private EnterFactoryTabFragment mEnterFactoryTabFragment;
    private int total;
    private View mEmptyView;


    public static LeverFactoryFragment newInstance(int enterType, String param2) {
        LeverFactoryFragment fragment = new LeverFactoryFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, enterType);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            enterType = getArguments().getInt(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    protected LeverFactoryPesenter createPresenter() {
        return new LeverFactoryPesenter(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        mView = inflater.inflate(R.layout.fragment_enter_factorys, container, false);
        unbinder = ButterKnife.bind(this, mView);
        initView();
        initListener();

        return mView;
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        lrecyclerView.forceToRefresh();
    }

    @Override
    protected void initView() {
        mData= new ArrayList<>();
         mEmptyView= mView.findViewById(R.id.empty_view);
        //lrecyclerView.setEmptyView(mEmptyView);//设置在setAdapter之前才能生效
        initLinearRecyclerView(lrecyclerView);
        mLeveFactoryAdapter= new LeveFactoryAdapter(_mActivity, mData,enterType);
        mLRecyclerViewAdapter= new LRecyclerViewAdapter(mLeveFactoryAdapter);
        lrecyclerView.setAdapter(mLRecyclerViewAdapter);

        IPage page = new IPage() {

            @Override
            public void load(int geIndex, int pageSize) {
                mvpPresenter.enterFactoryNotTake(geIndex, pageSize, enterType);


            }
        };
        mPageWrapper= new PageWrapper<>(mIAdapter, page);


    }

    IAdapter mIAdapter = new IAdapter() {
        @Override
        public void setDataSource(List data, boolean isNextLoad) {
            mEmptyView.setVisibility(CheckUtils.isEmpty(data)?View.VISIBLE:View.GONE);
            mLeveFactoryAdapter.getDataList().clear();
            mLeveFactoryAdapter.setDataList(data);
            mLRecyclerViewAdapter.notifyDataSetChanged();
            lrecyclerView.setLoadMoreEnabled(isNextLoad);


        }

        @Override
        public void loadMoreFinish(List data, boolean isNextLoad) {

            mLeveFactoryAdapter.addAll(data);
            mLRecyclerViewAdapter.notifyDataSetChanged();
            if (!isNextLoad) lrecyclerView.setNoMore(!isNextLoad);

        }
    };


    @Override
    protected void initListener() {
        super.initListener();
        lrecyclerView.setOnRefreshListener(mOnRefreshListener);
        lrecyclerView.setOnLoadMoreListener(mOnLoadMoreListener);


    }


    private OnRefreshListener mOnRefreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh() {


            mPageWrapper.loadPage(true);
        }
    };
    private OnLoadMoreListener mOnLoadMoreListener = new OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
            mPageWrapper.loadPage(false);
        }
    };


    @Override
    public void enterFactorySucceed(EnterFactoryBean mEnterFactoryBean) {
        if(getParentFragment() instanceof TakeLetterTabFragment) {
            mEnterFactoryTabFragment= ((EnterFactoryTabFragment) getParentFragment());
            total= mEnterFactoryBean.getTotal();
            switch (enterType) {
                case 1: /*批次未送店*/
                    mEnterFactoryTabFragment.updataSendStore(mEnterFactoryBean.getTotal());
                    break;
                case 3: /*订单批次未送店*/
                    mEnterFactoryTabFragment.updataSendStore(mEnterFactoryBean.getTotal());
                    break;


            }
        }


        mPageWrapper.addDataSource(mEnterFactoryBean.getList(), mEnterFactoryBean.getPages());

    }

    @Override
    public void loadfinish() {
        lrecyclerView.refreshFinish();
    }

}
